﻿@using Pay.DTO
@using Pay.TO
@{
    Layout = null;
    string wxOpenId = ViewBag.WXOpenId;
    var merchant = ViewBag.Merchant as merchant_dto;
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>购买课程</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    <link href="~/Content/css/tokenmall/memberUpgrade/css/index.css" rel="stylesheet" />
    <script src="~/Scripts/token-common.js"></script>
    <script src="~/Scripts/tokenmall/jquery-1.8.3.min.js"></script>
    <link href="~/Content/js/layer/1.9.3/layui.css" rel="stylesheet" />
    <script src="~/Content/js/layer/1.9.3/layer.js"></script>
</head>
<body>
    <div class="publicity">
        <img src="~/Content/css/tokenmall/memberUpgrade/images/publicity.png?t=0001" />
        <input type="hidden" id="wxOpenId" value="@wxOpenId" />
        @if (merchant.usertype == UserType.user) {
            <a href="javascript:confirmToPay();" class="apply">立即报名</a>
        } else {<!-- 已报名状态 -->
            <a href="javascript:void(0)" class="apply hasapply">已报名</a>
        }
    </div>
</body>
</html>


<script type="text/javascript">
    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                //移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
                WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };
    $(function () {
        var wxOpenId = $("#wxOpenId").val();
        if (isWxBrowser() == true && (wxOpenId == undefined || wxOpenId == "")) {
            window.location.href = '@Url.Action("GetWXOpenIdAuth", "CommonTokenMall")?returnUrl=@System.Web.HttpUtility.UrlEncode(Request.Url.ToString())';
        }
        if (isWxBrowser() == false && wxOpenId != undefined && wxOpenId != "") {
            $("#wxOpenId").val('');
            wxOpenId = "";
        }


        //防止页面后退
        history.pushState(null, null, document.URL);
        window.onpopstate = function (e) {
            if (browser.versions.mobile) {
                //判断是否是移动设备打开。browser代码在下面
                var ua = navigator.userAgent.toLowerCase();
                //获取判断用的对象
                if (ua.match(/MicroMessenger/i) == "micromessenger") { //微信app
                    history.pushState(null, null, document.URL);
                    this.window.location.replace('@Url.Action("Index", "Consumption")');
                } else {//非微信打开
                    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                        try {
                            if (isIOSAppNewBrowser != undefined && isIOSAppNewBrowser == true) {
                                window.webkit.messageHandlers.gohome.postMessage(''); 
                            } else {
                                gohome();
                            }
                        }
                        catch (ex) { }
                    }
                    else if (/(Android)/i.test(navigator.userAgent)) {
                        try { androidfuntion.gohome(); }
                        catch (ex) { }
                    }
                }
            } else {
                //否则就是PC浏览器打开
                history.pushState(null, null, document.URL);
                this.window.location.replace('@Url.Action("Index", "Consumption")');
            }
        }
    
    });

    var isConfirmToPayDisable = false;
    function confirmToPay() {
            if (!isConfirmToPayDisable) {
                isConfirmToPayDisable = true;
                $.ajax({
                    type: 'POST',
                    async: false,
                    url: '/MemberSpace/UpgradeTokenMakerPay',
                    data: { wx_open_id: $("#wxOpenId").val() },
                    success: function (result) {
                        if (result.result_code == 1000) {
                            if (isWxBrowser() == true) {//微信浏览器打开，公众号支付
                                var wcPayModel = $.parseJSON(result.result_msg);
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    onBridgeReady(wcPayModel);
                                }
                            } else {//h5支付
                                window.location.href = result.result_msg;
                            }
                        }
                        else {
                            layer.msg(result.result_msg);
                            isConfirmToPayDisable = false;
                        }
                    },
                    error: function () {
                        isConfirmToPayDisable = false;
                    }
                });
            }
    }

            function onBridgeReady(wcPayModel) {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": wcPayModel.appid,
                    "timeStamp": wcPayModel.timestamp,
                    "nonceStr": wcPayModel.noncestr,
                    "package": wcPayModel.package,
                    "signType": "MD5",
                    "paySign": wcPayModel.paysign
                },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                        window.location.href = '@Url.Action("UpgradeTokenMaker", "MemberSpace")';
                    } else {
                        window.location.href = '@Url.Action("UpgradeTokenMaker", "MemberSpace")';
                    }
                }
            );
        }

</script>